#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <malloc.h>
#include <errno.h>
#include "ip2loc.h"
int split2str(char * str,char * sep,char * buf,int linesize);
int main(int argc, char* argv[])
{
    char* loadsrc;
    FILE* fp;
    key_t key;
    int shm_id;
    unsigned long start=0;
    unsigned long end=0;
    int total;
    char row[1024+1]={0};
    char opt[7][128];
    iploc * shm_p = NULL;
    iploc * loc_p = NULL;

    if (argc != 2)
    {
 	printf("Usage:%s src\n", argv[0]);
	return 1;
    }

    loadsrc = argv[1];
    printf("Load from %s\n", loadsrc);

    fp = fopen(loadsrc, "r");
    if (!fp)
    {
	printf("Open file[%s] failed!\n", loadsrc);
	return 2;
    }
    key = ftok(SHMFILENAME, 0);
    if (key == -1)
    {
    	perror("Ftok error");
    	return 2;
    }
    #define SHM_SIZE (sizeof(iploc) * IPCNT)

    printf("iploc size:%d, shmsize:%d\n",(int)sizeof(iploc),(int)SHM_SIZE);

    shm_id = shmget(key, SHM_SIZE, IPC_CREAT|0666);
    if (shm_id == -1)
    {
    	printf("Create shared memory failed,%s\n",strerror(errno));
    	return 3;
    }

    printf("shmget ok\n");

    shm_p = (iploc *)shmat(shm_id, NULL, 0);
    if (shm_p == NULL)
    {
      	perror("shmat Failed!");
        return 4;
    }

    printf("shmnat ok\n");

    loc_p = shm_p;
    total = 0;

    while (!feof(fp))
    {
	    if(total >= IPCNT)
        {
            printf("exceed to %d, drop data,exit\n",total);
            break;
        }
        memset(row,0,sizeof(row));
        fgets(row, 1024, fp);

        if (row[0] == '#' || strlen(row) == 0)
            continue;

        row[strlen(row)-1] = 0;
	    //printf("total:%d, len:%d, row:%s\n",(int)total, (int)strlen(row), row);

	    split2str(row,"|",(char*)opt,sizeof(opt[0]));

	    //printf("startip:%s,endip:%s,country:%s,isp:%s,province:%s,city:%s,regionid:%s\n",opt[0],opt[1],opt[2],opt[3],opt[4],opt[5],opt[6] );

        start = ip2long(opt[0]);
        end = ip2long(opt[1]);

        loc_p->start = start;
        loc_p->end = end;
        strcpy(loc_p->country,opt[2]);
        strcpy(loc_p->isp,opt[3]);
        strcpy(loc_p->province,opt[4]);
        strcpy(loc_p->city,opt[5]);
        loc_p->regionid = atoi(opt[6]);

        loc_p++;
        total++;

	    //printf("load total =%d\n", total);

    }

    //qsort(loc, IPCNT, sizeof(iploc), ipcmp);
    printf("load all total: %d....\n", total);
    fclose(fp);
    shmdt(shm_p);
    printf("load all total: %d\n", total);
    return 0;
}

int split2str(char * str,char * sep,char * buf,int linesize)
{
    int    i = 0;
    int    nLen = 0;
    char * p = NULL;
    char * pszBuf = NULL; 
	
    if(str == NULL || strlen(str) == 0 || sep == NULL || strlen(sep) == 0 || buf == NULL)
    {
	return -1;
    }

    nLen = strlen(str) + 1;
    pszBuf = (char*)malloc(nLen);
    strcpy(pszBuf,str);
    p = strtok(pszBuf,sep);
	
    while(p != NULL)
    {
        strcpy((char*)(buf+i*linesize),p);
	p=strtok(NULL,sep);
	i++;
    }
    free(pszBuf);

    return i;
}

unsigned long ip2long(char* ip)
{
    struct in_addr addr;
    int ret = inet_aton(ip, &addr);
    if (ret == 0)
	return 0;

    return (unsigned long) ntohl(addr.s_addr);
}

